﻿using System;
using System.Configuration;
using System.Data;
using System.Reflection;
using CommonLib.Log;

namespace CommonLib.Log
{
    /// <summary>
    /// 文本日志类型
    /// </summary>
    public enum LogType
    {
        [Text("错误")]
        E = 0,
        [Text("DB类")]
        SQL = 1,
        [Text("提示")]
        I = 2,
        [Text("警告")]
        A = 3
    }

    /// <summary>
    /// 消息状态
    /// </summary>
    public enum MessageStatus
    {
        [Text("未处理")]
        UnDo = 0,
        [Text("已处理")]
        Done = 1
    }

    /// <summary>
    /// 反馈结果/意见
    /// </summary>
    public enum FBResult
    {
        [Text("NG")]
        NG = 0,
        [Text("OK")]
        OK = 1
    }




    public class EnumManager
    {
        public static string GetEnumTextVal(int enumConst, Type enumType)
        {
            string textVal = string.Empty;

            try
            {
                if (!enumType.IsEnum)
                {
                    throw new InvalidOperationException();
                }

                Type typeDescription = typeof(TextAttribute);
                FieldInfo fieldInfo = enumType.GetField(Enum.GetName(enumType, enumConst).ToString());

                if (fieldInfo != null)
                {
                    object[] arr = fieldInfo.GetCustomAttributes(typeDescription, true);
                    if (arr.Length > 0)
                    {
                        TextAttribute textAttribute = (TextAttribute)arr[0];
                        textVal = textAttribute.Text;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.getInstance().WriteToLog((int)LogType.E, "GetEnumTextVal => " + ex.Message, ConfigurationManager.AppSettings["CommonLog"].ToString());
            }

            return textVal;
        }

        public static DataTable GetEnumTable(Type enumType)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Text", typeof(System.String));
            dt.Columns.Add("Value", typeof(System.String));

            try
            {
                if (!enumType.IsEnum)
                {
                    throw new InvalidOperationException();
                }

                Type typeDescription = typeof(TextAttribute);

                FieldInfo[] fields = enumType.GetFields();

                foreach (FieldInfo field in fields)
                {
                    if (field.FieldType.IsEnum == true)
                    {
                        DataRow dr = dt.NewRow();

                        dr["Value"] = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();

                        object[] arr = field.GetCustomAttributes(typeDescription, true);
                        if (arr.Length > 0)
                        {
                            TextAttribute aa = (TextAttribute)arr[0];
                            dr["Text"] = aa.Text;
                        }
                        else
                        {
                            dr["Text"] = field.Name;
                        }
                        dt.Rows.Add(dr);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.getInstance().WriteToLog((int)LogType.E, "GetEnumTable => " + ex.Message, ConfigurationManager.AppSettings["CommonLog"].ToString());
            }

            return dt;
        }

        public static DataTable GetEnumTableForText(Type enumType, string text)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Text", typeof(System.String));
            dt.Columns.Add("Value", typeof(System.String));

            try
            {
                if (!enumType.IsEnum)
                {
                    throw new InvalidOperationException();
                }

                FieldInfo[] fields = enumType.GetFields();

                foreach (FieldInfo field in fields)
                {
                    if (field.FieldType.IsEnum == true && enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null).ToString() == text)
                    {
                        DataRow dr = dt.NewRow();

                        dr["Value"] = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
                        dr["Text"] = enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null).ToString();
                        dt.Rows.Add(dr);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.getInstance().WriteToLog((int)LogType.E, "GetEnumTableForText => " + ex.Message, ConfigurationManager.AppSettings["CommonLog"].ToString());
            }

            return dt;
        }
    }
}
